﻿//-----------------------------------------------------------------------
// <copyright file="GenericCommandWrapper.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.UI.Commands
{
    using System.Windows.Input;

    /// <summary>
    /// A command wrapper to redirect to a command with a stored parameter
    /// Useful to be able to hand out commands that refer back to a view model
    /// </summary>
    /// <typeparam name="T">the wrapped type param</typeparam>
    /// <typeparam name="U">The surfaced param type</typeparam>
    public class GenericCommandWrapper<T, U> : GenericCommandBase<U> where U : class
    {
        /// <summary>
        /// The wrapped command
        /// </summary>
        private readonly ICommand wrappedCommand;

        /// <summary>
        /// The wrapped command's param
        /// </summary>
        private readonly T wrappedCommandParameter;

        /// <summary>
        /// Initializes a new instance of the GenericCommandWrapper class
        /// </summary>
        /// <param name="wrappedCommand">the wrapped command</param>
        /// <param name="wrappedCommandParameter">the parameter to the wrapped command</param>
        public GenericCommandWrapper(ICommand wrappedCommand, T wrappedCommandParameter)
        {
            this.wrappedCommand = wrappedCommand;
            this.wrappedCommandParameter = wrappedCommandParameter;
        }

        /// <summary>
        /// Execute the command
        /// </summary>
        /// <param name="value">the command parameter</param>
        public override void ExecuteOnValue(U value)
        {
            this.wrappedCommand.Execute(this.wrappedCommandParameter);
        }

        /// <summary>
        /// Test if the command can execute
        /// </summary>
        /// <param name="value">the command parameter</param>
        /// <returns>true if the command can execute</returns>
        public override bool CanExecuteOnValue(U value)
        {
            return this.wrappedCommand.CanExecute(this.wrappedCommandParameter);
        }
    }
}
